﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IRIS.Models;
using System.Net.Mail;
using System.Configuration;

namespace IRIS.Controllers
{

    [HandleError]
    public class NewsletterController : Controller
    {

         DataRepo rep = new DataRepo();
        //
        // GET: /Newsletter/
        
        
        //This function is for Add on _Layout page. There is no view for Add but this is needed because
        // the html code send information to the add function
        [HttpGet]      
        public ActionResult Add()
        {

            return View();
        }
        
        [HttpPost]
        public ActionResult Add(Newsletter Formdata)
        {
            if (string.IsNullOrEmpty(Formdata.Email))
                ModelState.AddModelError("Email", "Þessi reitur verður að vera útfylltur");
            try
            {
                if (ModelState.IsValid)
                {
                   
                    rep.addNewsletter(Formdata);
                    return View("AddSuccess");
                }
                else
                {
                    
                    return View(Formdata);
                }
            }
            catch
            {
               
                return View(Formdata);
            }
        }

        //This function is Create Newsletter Form
        [HttpGet]
        public ActionResult List()
        {
            return View();
        }

        [Authorize(Roles = "Stjórnendur")]
        [ValidateInput(false)] 
        [HttpPost]
        public ActionResult List(NewsletterModel Formdata)
        {
            var email = rep.getNewsletters();
            try
                {
                    if (email != null)
                    {
                            // include System.Net.Mail in library
                            MailMessage mail = new MailMessage();  
                            // This is our sender Email
                            mail.From = new MailAddress("hopur14@gmail.com"); 
                            
                            //Subject and body in the newsletter
                            mail.Subject = Formdata.subject;
                            mail.Body = Formdata.letter; 
                            
                            // Loop through the emails in the database and Add them
                            // to Sending list
                            foreach (var emailAddress in email )
                            {
                                 mail.To.Add(emailAddress.Email);
                            }
                            
                            //Client SMTP for google
                            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
                            // This must be included
                            smtp.EnableSsl = true; 
                            //Send mail
                            smtp.Send(mail);
                      }

                         return View("Success");
                     }            
            
                catch 
                {
                    return View("NotFound");
                }
            }


        [HttpGet]
        public ActionResult Remove()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Remove(Newsletter theData)
        {

            if (string.IsNullOrEmpty(theData.Email))
                ModelState.AddModelError("Email", "Þessi reitur verður að vera útfylltur");
            try
            {
                if (ModelState.IsValid)
                {

                    rep.deleteNewsletter(theData.Email);
                    return View("RemoveSuccess");
                }
                else
                {

                    return View(theData);
                }
            }
            catch
            {

                return View(theData);
            }
        }
    }
}

